package com.venue.hjt.venueclass.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.venue.hjt.venueclass.R;
import com.venue.hjt.venueclass.bean.VenueBooKingDetaitBean;
import com.venue.hjt.venueclass.ui.FaceTrackingActivity;
import com.venue.hjt.venueclass.view.CircleImageView;

import java.util.List;

/**
 * Created by xqx on 2017/10/25.
 */

public class VenueSignAdapter extends BaseAdapter {
    private Context mContext;
    private List<VenueBooKingDetaitBean.Total> list;
    private int type;
    private String tid;
    private boolean isSign=true;

    public VenueSignAdapter(Context mContext, List<VenueBooKingDetaitBean.Total> list,int type,String tid) {
        this.mContext = mContext;
        this.list = list;
        this.type=type;
        this.tid=tid;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHander viewHander;
        if (convertView==null){
            viewHander=new ViewHander();
            convertView=View.inflate(mContext, R.layout.venue_sign_item,null);
            viewHander.iv_student_url=convertView.findViewById(R.id.iv_student_url);
            viewHander.tv_name=convertView.findViewById(R.id.tv_name);
            viewHander.tv_sign=convertView.findViewById(R.id.tv_sign);
            convertView.setTag(viewHander);

        }else{
            viewHander= (ViewHander) convertView.getTag();
        }
        final VenueBooKingDetaitBean.Total total = list.get(position);
        if (total.headimg!=null){
            Glide.with(mContext).load(total.headimg).into(viewHander.iv_student_url);
        }
        if (type==0){
            viewHander.tv_sign.setVisibility(View.VISIBLE);
            viewHander.tv_sign.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isSign){
                        Intent intent=new Intent(mContext, FaceTrackingActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("id",tid+total.student_no);
                        mContext.startActivity(intent);
                        isSign=false;
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                isSign=true;
                            }
                        },5000);
                    }
                }
            });
        }
        viewHander.tv_name.setText(total.name);

        return convertView;
    }
    class ViewHander{
        private TextView tv_name,tv_sign;
        CircleImageView iv_student_url;
    }

    @Override
    public boolean isEnabled(int position) {
        return false;
    }
}
